﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace D2DEngine.Util
{
    public static class D2DMath
    {
        private static Random random = new Random();
        private const float pixelsToMeterRatio = 100.0f;

        public static Vector2 ToMeters(Vector2 pixels)
        {
            return new Vector2(pixels.X / pixelsToMeterRatio, pixels.Y / pixelsToMeterRatio);
        }

        public static float ToMeters(float pixels)
        {
            return pixels / pixelsToMeterRatio;
        }

        public static Vector2 ToPixels(Vector2 pixels)
        {
            return new Vector2(pixels.X * pixelsToMeterRatio, pixels.Y * pixelsToMeterRatio);
        }

        public static float ToPixels(float meters)
        {
            return meters * pixelsToMeterRatio;
        }

        public static int DivisionCeiling(int n1, int n2)
        {
            return (int)Math.Ceiling(Convert.ToDecimal(n1) / Convert.ToDecimal(n2));
        }

        public static int RandomInt(int min, int max)
        {
            return random.Next(min, max);
        }

        public static float RandomFloat(float min, float max)
        {
            return min + (float)random.NextDouble() * (max - min);
        }

        private static bool LineSegmentIntersection(Vector2 point1, Vector2 point2, Vector2 point3, Vector2 point4)
        {
            float ua = (point4.X - point3.X) * (point1.Y - point3.Y) - (point4.Y - point3.Y) * (point1.X - point3.X);
            float ub = (point2.X - point1.X) * (point1.Y - point3.Y) - (point2.Y - point1.Y) * (point1.X - point3.X);
            float denominator = (point4.Y - point3.Y) * (point2.X - point1.X) - (point4.X - point3.X) * (point2.Y - point1.Y);

            //intersection = coincident = false;
            Vector2 intersect = Vector2.Zero;

            if (Math.Abs(denominator) <= 0.00001f)
            {
                if (Math.Abs(ua) <= 0.00001f && Math.Abs(ub) <= 0.00001f)
                {
                    /*
                    intersection = coincident = true;
                    intersect = (point1 + point2) / 2;
                     */
                    return true;
                }
            }
            else
            {
                ua /= denominator;
                ub /= denominator;

                if (ua >= 0 && ua <= 1 && ub >= 0 && ub <= 1)
                {
                    /*
                    intersection = true;
                    intersect.X = point1.X + ua * (point2.X - point1.X);
                    intersect.Y = point1.Y + ua * (point2.Y - point1.Y);
                     */
                    return true;
                }
            }

            return false;
        }

        public static bool wow()
        {
            return true;
        }
    }
}
